<template>
  <div>
    <el-row>
      <el-col :span="12">
        <ul class="moneyBox">
          <li>
            <p>{{ $t('风险敞口金额') + $t('金额单位') }}</p>
            <span>{{ moneyFormat(riskSum ? riskSum.riskExposureSum:0, 'w') }}</span>
          </li>
          <li>
            <p>{{ $t('损失金额') + $t('金额单位') }}</p>
            <span>{{ moneyFormat(riskSum ? riskSum.riskLossSum:0, 'w') }}</span>
          </li>
        </ul>
      </el-col>
    </el-row>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('项目名称')">
        <el-input v-model="queryParams.dealName" placeholder="项目名称" />
      </el-form-item>
      <el-form-item :label="$t('风险类型')">
        <dictSelect
          v-model="queryParams.riskType"
          size="small"
          :clearable="true"
          dictionary="risk_type"
        />
      </el-form-item>
      <el-form-item :label="$t('风险状态')">
        <dictSelect
          v-model="queryParams.riskState"
          size="small"
          :clearable="true"
          dictionary="risk_status"
        />
      </el-form-item>
      <el-form-item :label="$t('风险名称')">
        <el-input v-model="queryParams.riskName" placeholder="风险名称" />
      </el-form-item>
      <el-form-item :label="$t('发生时间')">
        <!-- <el-date-picker
          v-model="queryParams.date"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        /> -->
        <DatePicker
          v-model="dateRange"
          type="daterange"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        <el-button type="warning" icon="el-icon-download" size="mini" @click="exportHandle">{{ $t('导出') }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" border :data="tableData" style="width: 100%">
      <el-table-column prop="dealName" label="项目名称">
        <template slot-scope="scope">
          <router-link
            target="_blank"
            :to="{path:'/deal/home/dealBaseHome',query:{id:scope.row.dealId, companyId: scope.row.companyId}}"
            class="link_a"
          >{{ scope.row.dealName }}</router-link>
        </template>
      </el-table-column>
      <el-table-column prop="riskType" label="风险类型" />
      <el-table-column prop="riskName" label="风险名称">
        <template slot-scope="scope">
          <el-button type="text" @click="updateHandle(scope.row)">{{ scope.row.riskName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="riskState" label="风险状态" />
      <el-table-column prop="riskTime" label="发生时间">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.riskTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="riskExposure" :label="$t('风险敞口金额') + $t('金额单位')" align="right">
        <template slot-scope="scope">
          {{ moneyFormat(scope.row.riskExposure, 'w') }}
        </template>
      </el-table-column>
      <el-table-column prop="riskLoss" :label="$t('损失金额') + $t('金额单位')" align="right">
        <template slot-scope="scope">
          {{ moneyFormat(scope.row.riskLoss, 'w') }}
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="风险事项描述" show-overflow-tooltip />
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改风险管理对话框 -->
    <el-dialog
      title="风险管理"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      append-to-body
    >
      <dealRiskForm ref="dealRiskForm" :outside="false" />
    </el-dialog>
  </div>
</template>

<script>
import { getRiskStatisticsReport } from '@/api/deal/risk/dealRisk'
import dealRiskForm from './dealRiskForm.vue'

export default {
    components: {
        dealRiskForm
    },
  data() {
    return {
      queryParams: {
          dealName: undefined,
          riskType: undefined,
          riskState: undefined,
          riskName: undefined,
          date: undefined,
          pageNum: 1,
          pageSize: 10
      },
      tableData: [],
      formOpen: false,
      total: 0,
      riskSum: {},
      loading: false,
      dateRange: undefined
    }
  },
  created() {
        this.getList()
  },
  methods: {
    queryHandle() {
       this.getList()
    },
    resetQuery() {
      this.queryParams = {
          dealName: undefined,
          riskType: undefined,
          riskState: undefined,
          riskName: undefined,
          pageNum: 1,
          pageSize: 10
      }
       this.dateRange = undefined
       this.queryHandle()
    },
    getList() {
        this.loading = true
        getRiskStatisticsReport(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          if (response.code === 200) {
              this.tableData = response.data.rows
              this.total = response.data.total
              this.riskSum = response.data.sum
          }
           this.loading = false
        })
    },
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealRiskForm.init(row)
        })
    },
    exportHandle() {
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        this.downLoadExcel('/risk/dealRisk/getRiskStatisticsReport/export', this.addDateRange(this.queryParams, this.dateRange))
      }).catch(() => {
      })
    }
  }
}
</script>

<style>

</style>
